package org.eae.eldf.webapp.controller.sys;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContext;

import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.webapp.common.bean.Result;
import org.eae.eldf.webapp.common.bean.ResultList;
import org.eae.eldf.webapp.common.constant.StateDefine;
import org.eae.eldf.dto.module.ModuleInfoQueryDto;
import org.eae.eldf.dto.sys.SysPurviewQueryDto;
import org.eae.eldf.entity.module.ModuleInfo;
import org.eae.eldf.entity.sys.SysMenu;
import org.eae.eldf.entity.sys.SysOperator;
import org.eae.eldf.entity.sys.SysPurview;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.framework.spring.SpringSecurityUtils;
import org.eae.eldf.framework.annotation.Token;
import org.eae.eldf.service.sys.SysMenuService;
import org.eae.eldf.service.sys.SysPurviewService;

@Controller
public class SysMenuPurviewController extends AbstractController<SysPurview> {

	private SysPurviewService purviewService;

	@Autowired
	public void setPurviewService(SysPurviewService purviewService) {
		this.purviewService = purviewService;
	}

	private SysMenuService menuService;

	@Autowired
	public void setMenuService(SysMenuService menuService) {
		this.menuService = menuService;
	}

	/*	*//**
			 * 子菜单-权限管理列表
			 * 
			 * @param username
			 * @param sysPurviewId
			 * @param modelMap
			 * @param request
			 * @param response
			 * @return
			 *//*
			 * @RequestMapping(value = "/sys/menu-purview/list") public String
			 * purListPage(
			 * 
			 * @RequestParam(required=false) Long parentid,
			 * 
			 * @RequestParam(required=false) Long menParentId, ModelMap
			 * modelMap,HttpServletRequest request,HttpServletResponse
			 * response){ Pagination<SysPurview>
			 * pageList=this.initPager(request, response);
			 * pageList=purviewService.purList(pageList, parentid); if (null !=
			 * parentid) { SysMenu menu = menuService.get(SysMenu.class,
			 * parentid); modelMap.addAttribute("menu", menu); }
			 * modelMap.addAttribute("list",pageList);
			 * modelMap.addAttribute("menuId",parentid);
			 * modelMap.addAttribute("menParentId",menParentId); return
			 * "/sys/menu-purview/list"; }
			 */

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param searchValue
	 * @return
	 */
	@RequestMapping(value = "/sys/menu-purview/list")
	public @ResponseBody ResultList<SysPurview> pageList(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "sysPurviewQueryDto") SysPurviewQueryDto sysPurviewQueryDto) {
		Pagination<SysPurview> pager = super.newPaginationInstance(sysPurviewQueryDto);
		pager = purviewService.queryPagePurviewList(pager, sysPurviewQueryDto);
		return outPageArrayList(pager, sysPurviewQueryDto);
	}

	/*	*//**
			 * 子菜单-权限管理 :跳转权限页面
			 * 
			 * @param id
			 * @param modelMap
			 * @param username
			 * @param spurname
			 * @param surlpath
			 * @param sdescription
			 * @return
			 *//*
			 * @RequestMapping(value="/sys/menu-purview/form")
			 * 
			 * @Token(save=true) public String
			 * toMenuPurPage(@RequestParam(required=false) Long id,
			 * 
			 * @RequestParam(required=false) Long menParentId,
			 * 
			 * @RequestParam(required=false) Long menuId, ModelMap modelMap){
			 * SysPurview sysPurview=null; if(null!=id){
			 * sysPurview=purviewService.getById(id); } else{ sysPurview=new
			 * SysPurview(); } modelMap.addAttribute("syspurview",sysPurview);
			 * modelMap.addAttribute("menuId",menuId);
			 * modelMap.addAttribute("menParentId",menParentId); return
			 * "/sys/menu-purview/form"; }
			 */

	/**
	 * 新增菜单
	 * 
	 * @param operator
	 * @return
	 */
	@RequestMapping(value = "/sys/menu-purview/add")
	@ResponseBody
	public Result insert(HttpServletRequest request, @ModelAttribute(value = "sysPurview") SysPurview sysPurview) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		RequestContext requestContext = new RequestContext(request);
		if (purviewService.queryCount(
				Criteria.create(SysPurview.class).where("spurname", new Object[] { sysPurview.getSpurname() })) > 0) {
			result.setResultValue(false);
			result.setMessage(requestContext.getMessage("menupur.msg.spurname.exist"));
		} else {
			purviewService.insert(Criteria.create(SysPurview.class).set("parentid", sysPurview.getParentid())
					.set("spurname", sysPurview.getSpurname()).set("surlpath", sysPurview.getSurlpath())
					.set("sdescription", sysPurview.getSdescription()).set("dadddate", new Date())
					.set("dcreatetime", new Date()).set("dlastupdatetime", new Date()));
		}

		return result;
	}

	/**
	 * 编辑菜单
	 * 
	 * @param operator
	 * @return
	 */
	@RequestMapping(value = "/sys/menu-purview/update")
	@ResponseBody
	public Result update(HttpServletRequest request, @ModelAttribute(value = "sysPurview") SysPurview sysPurview) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		RequestContext requestContext = new RequestContext(request);
		if (purviewService.queryCount(
				Criteria.create(SysPurview.class).where("spurname", new Object[] { sysPurview.getSpurname() })) > 0) {
			result.setResultValue(false);
			result.setMessage(requestContext.getMessage("menupur.msg.spurname.exist"));
		} else {
			purviewService.update(Criteria.create(SysPurview.class).set("parentid", sysPurview.getParentid())
					.set("spurname", sysPurview.getSpurname()).set("surlpath", sysPurview.getSurlpath())
					.set("sdescription", sysPurview.getSdescription()).set("dlastupdatetime", new Date())
					.where("sysPurviewId", new Object[] { sysPurview.getSysPurviewId() }));
		}

		return result;
	}


	
	/**
	 * 删除用户
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/sys/menu-purview/del")
	public @ResponseBody
	Result delete(@RequestParam(required = false) Long id) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		purviewService.delete(id);
		return result;
	}

}
